/*

@Author:刘洋

@Date: 2018-12-12

@行政人员服务

@Last Modified time: 2018-12-16
*/
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
import { ApiService } from './api.service';

import { User } from '../domain/user';
import {SignInRecord} from '../domain/sign-in-record'
import {LeaveRequest} from '../domain/leave-request'
import {OutRequest} from '../domain/out-request'
import { UserConfirm } from '../domain/user-confirm';
@Injectable({
  providedIn: 'root'
})

export class AdministrativeService {
  private api_url;
  private headers;
  constructor(private apiService: ApiService,
    private http: HttpClient) {
    this.api_url = apiService.getUrl();
    this.headers = apiService.getHeaders();
    }
  getUsers (): Observable<User[]> {
    return this.http.get<User[]>(this.api_url + "/users")
    .pipe(
      catchError(this.handleError('getUsers', []))
    );
  }
  getLeaveRequest(): Observable<LeaveRequest[]>{
    return this.http.get<LeaveRequest[]>(this.api_url + "/leave_requests")
    .pipe(
      catchError(this.handleError('getLeaveRequest', []))
    );
  }
  getOutRequest(): Observable<OutRequest[]>{
    return this.http.get<OutRequest[]>(this.api_url + "/out_requests")
    .pipe(
      catchError(this.handleError('getOutRequest', []))
    );
  }
  private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      console.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
  updateUser(user:User): Observable<any> {
    return this.http.put(this.api_url + "/users/"+user.id, user, this.headers).pipe(
      tap(_ => console.log(`updated user id=${user.id}`)),
      catchError(this.handleError<any>('updateUser'))
    );
  }
  getUser(id): Observable<User[]> {
    return this.http.get<User[]>(this.api_url + '/users?id=' + id)
      .pipe(
        catchError(this.handleError('getUsers', []))
      );
    }
  getUserLike(id): Observable<User[]> {
    return this.http.get<User[]>(this.api_url + '/users?id_like=' + id)
      .pipe(
        catchError(this.handleError('getUsers', []))
      );
    }
  searchSign(startday:string,endday:string,ID:string):Observable<SignInRecord[]>{
    var url = this.api_url+"/sign_in_records?";
    if(startday.length!==0){
      url+="day_gte=" + startday + "&";}
    if(endday.length !==0){
      url+="day_lte=" + endday + "&";}
    if(ID.length !== 0){
      url+="user_id=" + ID + "&";
    }
    return this.http.get<SignInRecord[]>(url)
    .pipe(
      catchError(this.handleError('searchSign', []))
    );
  }
  addUser(user:User): Observable<any> {
    return this.http.post(this.api_url + "/users", user, this.headers).pipe(
      tap(_ => console.log(`add user id=${user.id}`)),
      catchError(this.handleError<any>('addUser'))
    );
  }
  addUserConfirm(userConfirm:UserConfirm): Observable<any> {
    return this.http.post(this.api_url + "/user_confirms", userConfirm, this.headers).pipe(
      tap(_ => console.log(`add userConfirm id=${userConfirm.id}`)),
      catchError(this.handleError<any>('addUserConfirm'))
    );
  }
  deleteUser (user:User): Observable<any> {
    return this.http.delete(this.api_url + "/users/" + user.id)
    .pipe(
      catchError(this.handleError('deleteUser', []))
    );
  }
  deleteUserConfirm (id:number): Observable<any> {
    return this.http.delete(this.api_url + "/user_confirms/" + id)
    .pipe(
      catchError(this.handleError('deleteUserConfirm', []))
    );
  }
  getLeaveUser(id): Observable<LeaveRequest[]> {
    return this.http.get<LeaveRequest[]>(this.api_url + '/leave_requests?user_id_like=' + id)
      .pipe(
        catchError(this.handleError('getLeaveUser', []))
      );
    }
  getOutUser(id): Observable<OutRequest[]> {
    return this.http.get<OutRequest[]>(this.api_url + '/out_requests?user_id_like=' + id)
      .pipe(
        catchError(this.handleError('getOutUser', []))
      );
    }
}
